package com.chen.hotel.controller;


import com.chen.hotel.Service.RoomService;
import com.chen.hotel.VO.ResultVO;
import com.chen.hotel.annotation.LoginToken;
import com.chen.hotel.domain.Room;
import com.chen.hotel.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    /**
     * 创建一个房间
     * @param room
     * @return
     */
    @PostMapping("")
    @LoginToken
    public ResultVO createRoom(@RequestBody Room room){
        return ResultVOUtil.success(this.roomService.create(room));
    }

    /**
     * 更新一个房间信息
     */
    @PatchMapping("/{id}")
    @LoginToken
    public ResultVO updateRoom(@PathVariable Integer id ,@RequestBody Room room){
        room.setId(id);
        return ResultVOUtil.success(this.roomService.update(room));
    }

    /**
     * 删除一个房间
     */
    @DeleteMapping("/{id}")
    @LoginToken
    public ResultVO deleteRoom(@PathVariable Integer id){
        this.roomService.deleteById(id);
        return ResultVOUtil.success(null);
    }

    /**
     * 根据ID获取一个房间的信息
     */
    @GetMapping("/{id}")
    public ResultVO getRoomDetail(@PathVariable Integer id){
        return ResultVOUtil.success(this.roomService.getRoomById(id));
    }

    @GetMapping("")
    public ResultVO getAllRoom(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "size", defaultValue = "10") Integer size){
        PageRequest request = PageRequest.of(page, size);
        return ResultVOUtil.success(this.roomService.getAllRoom(request));
    }

    /**
     * 获取某种类型的全部房间列表
     */
    @GetMapping("/type/{type}")
    public ResultVO getRoomByType(@PathVariable Integer type,
                                  @RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "size", defaultValue = "10") Integer size){
        PageRequest request = PageRequest.of(page, size);
        return ResultVOUtil.success(this.roomService.getRoomByType(request, type));
    }

    /**
     * 分页获取某个楼层的所有房间列表
     */
    @GetMapping("/floor/{floor}")
    public ResultVO getRoomByFloor(@PathVariable Integer floor,
                                   @RequestParam(value = "page", defaultValue = "0") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size){
        PageRequest request = PageRequest.of(page, size);
        return ResultVOUtil.success(this.roomService.getRoomByFloor(request, floor));
    }

    /**
     * 获取特定状态下的房间列表
     */
    @GetMapping("/status/{status}")
    public ResultVO getRoomByStatus(@PathVariable Integer status,
                                    @RequestParam(value = "page", defaultValue = "0") Integer page,
                                    @RequestParam(value = "size", defaultValue = "10") Integer size){
        PageRequest request = PageRequest.of(page, size);
        return ResultVOUtil.success(this.roomService.getRoomByStatus(request, status));
    }
}
